package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.RoleConstants;
import com.mrd.constant.SystemConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.FilterGroupOption;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.business.entity.Contacter;
import com.mrd.gtimp.business.service.ContacterBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import javax.annotation.Resource;

import static com.mrd.util.AdminUtil.checkRight;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Controller
@RequestMapping("/business")
public class ContacterAction {
    private static final long serialVersionUID = 1L;

    @Resource
    private ContacterBiz contacterBiz;

    private String jsonString;

    private String k;

    /**
     * 默认访问入口
     */
    @RequestMapping("/contacter-main")
    public String main() {
        return "/gtimp/contacter/contacterList";
    }

    /**
     * 默认新增修改入口
     */
    @RequestMapping("/contacter-manage")
    public String manage() {
        return "/gtimp/contacter/contacterMange";
    }

    /**
     * 新增或修改信息
     */
    @RequestMapping("/contacter-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(User user, Contacter contacter) {
        Result msg;
        if (checkRight(RoleConstants.SUP_CUSTOMER_SAVE)) {
            try {
                if (StringUtils.isEmpty(contacter.getId())) {  // 新增操作
                    // contacter.setCOM_ID(user.getCompany().getId());
                    contacter.setCREATOR(user.getAccount());
                    contacter.setCTIME(new Date());
                    contacter.setId(null);
                } else {  // 修改操作
                    contacter.setMODIFIER(user.getAccount());
                    contacter.setMTIME(new Date());
                }
                contacterBiz.saveOrupdateEntity(contacter);
                msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
            } catch (Exception e) {
                e.printStackTrace();
                msg = Result.newFailInstance(MsgConstants.SAVE_FAIL + e.getMessage());
            }
            return msg;
        }
        return Result.newFailInstance(SystemConstants.ROLE_FAIL);
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/contacter-find")
    public Contacter find(Contacter contacter) {
        if (null != contacter) {
            if (StringUtils.isNotEmpty(contacter.getId())) {
                try {
                    contacter = contacterBiz.findEntityById(contacter.getId());
                    return contacter;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 删除信息
     */
    @RequestMapping("/contacter-del")
    @ResponseBody
    public Result del() {
        Result msg;
        Filter filters = null;
        if (checkRight(RoleConstants.SUP_CUSTOMER_DEL)) {
            try {
                String ids = HttpUtils.getParam("id");
                String[] idArr = ids.split(",");
                StringBuffer sb = new StringBuffer();
                for (String string : idArr) {
                    sb.append("'").append(string).append("',");
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                    filters = new Filter();
                    filters.setGroupOp(FilterGroupOption.AND);
                    filters.addRule(new SearchRule("ID", SearchRuleOption.in, sb.toString()));
                    contacterBiz.deleteByFilter(filters);
                }
                msg = Result.newSuccessInstance(MsgConstants.DEL_SUCCES);
            } catch (Exception e) {
                e.printStackTrace();
                msg = Result.newFailInstance(MsgConstants.DEL_FAIL + e.getMessage());
            }
            return msg;
        }
        return Result.newFailInstance(SystemConstants.ROLE_FAIL);
    }

    /**
     * 查询
     */
    @RequestMapping("/contacter-findList")
    @ResponseBody
    public Page<Contacter> findList(User user, Jqgrid jqgrid) {
        String comId = user.getCompany().getId();// get from user
        try {
//            if (null == filters) {
//                filters = new Filter();
//                filters.setGroupOp(FilterGroupOption.AND);
//            }
            SearchRule rule = new SearchRule("owner.id", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
//            page = initPage(contacterBiz.findEntityCount(filters));
//            Order order = new Order();
//            if (null != sidx && !"".equals(sidx)) {
//                order.add(sidx, sord);
//            }
//            contacters = contacterBiz.findByJqgrid(page.getCurrentPageStartNo(), page
//                    .getPageSize(), filters, order);
            Page<Contacter> contacters = contacterBiz.findByJqgrid(jqgrid);
            return contacters;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/contacter-findListByName")
    @ResponseBody
    public Page<Contacter> findListByName(User user, Jqgrid jqgrid) {
        String companyId = user.getCompany().getId();
        String name = HttpUtils.getParam("name");
//        if (null == filters) {
//            filters = new Filter();
//            filters.setGroupOp(FilterGroupOption.AND);
//        }
        try {
            SearchRule rule = new SearchRule("company", SearchRuleOption.cn, name);
            jqgrid.addRule(rule);
            SearchRule rule1 = new SearchRule("owner.id", SearchRuleOption.eq, companyId);
            jqgrid.addRule(rule1);
//            page = initPage(contacterBiz.findEntityCount(filters));
//            contacters = contacterBiz.findByJqgrid(page.getCurrentPageStartNo(), page
//                    .getPageSize(), filters, null);
            Page<Contacter> contacters = contacterBiz.findByJqgrid(jqgrid);
            return contacters;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Resource
    public void setContacterBiz(ContacterBiz contacterBiz) {
        this.contacterBiz = contacterBiz;
    }

    public String getJsonString() {
        return jsonString;
    }

    public void setJsonString(String jsonString) {
        this.jsonString = jsonString;
    }


    public void setK(String k) {
        this.k = k;
    }
}
